﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace CodeJam.ReverseWords
{
    class Program
    {
        static void Main(string[] args)
        {
            args = args.Length == 0 ? new string[] { "sample.txt" } : args;
            List<TestCase> testCases = TestCaseReader.ReadTestCases(args);
            if (testCases != null)
            {
                TestCaseReader.PrintTestCases(testCases);
            }

            Console.Read();
        }
    }

    public class TestCase
    {
        public List<string> Items { get; set; }
    }

    public class TestCaseReader
    {
        public static List<TestCase> ReadTestCases(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("must provide a test file.");
                return null;
            }

            string filename = args[0].ToString();

            if (!File.Exists(filename))
            {
                Console.WriteLine("test file: {0} doesn't exist.", filename);
                return null;
            }

            TextReader tr = new StreamReader(filename);
            List<TestCase> testcases = ReadTestCases(tr);

            tr.Close();

            return testcases;
        }

        private static List<TestCase> ReadTestCases(TextReader tr)
        {
            List<TestCase> testCases = new List<TestCase>();
            int numberTestCases = 0;
            int.TryParse(tr.ReadLine(), out numberTestCases);
            TestCase t;
            string[] items;
            
            for (int i = 0; i < numberTestCases; i++)
            {
                t = new TestCase();
                t.Items = new List<string>();
                items = tr.ReadLine().Split(' ');
                int j = items.Length - 1;

                foreach (var item in items)
                {
                    t.Items.Add(items[j]);
                    j--;
                }

                testCases.Add(t);
            }

            return testCases;
        }

        public static void PrintTestCases(List<TestCase> testCases)
        {
            TextWriter tw = new StreamWriter("testOutput.txt");
            int i = 1;
            string line = string.Empty;

            Console.WriteLine("Test Cases:");
            Console.WriteLine();
            foreach (TestCase item in testCases)
            {
                line = string.Format("Case #{0}: ", i);
                foreach (var word in item.Items)
                {
                    line = string.Concat(line + " " + word);
                }

                Console.WriteLine(line);
                tw.WriteLine(line);
                //PrintTestCase(item);
                i++;
            }

            tw.Close();
        }

        private static void PrintTestCase(TestCase item)
        {
            
        }
    }
}
